#define _DEFAULT_SOURCE
#include "app_runner.h"
#include <stdlib.h>
#include <signal.h>
#include "app_pool.h"
#include "app_serial.h"
#include "app_router.h"
#include "log/log.h"
#include <unistd.h>

int running = 1;

void stop_handler(int sig)
{
    if (sig==SIGINT || sig==SIGTERM)
    {
        running = 0;
    }
}

int app_runner_run()
{
    // 注册停止信息处理函数
    signal(SIGINT, stop_handler);
    signal(SIGTERM, stop_handler);

    if (app_pool_init(5)!= 0)
    {
        log_error("app pool init failed");
        return -1;
    }

    app_router_init();
    SerialDevice * serial = app_serial_init("/dev/ttyS1");
    app_serial_setConnectionType(serial, CONN_BT);
    app_router_registerDevice(&serial->super);

    while(running)
    {
        usleep(1000 * 200);
    }

    app_router_close();
    app_pool_close();

    return 0;
}